package com.yingside.jdbc.crm;

import java.sql.*;

public class DBHelper {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://47.108.158.110:3306/j181db";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    static{
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接
     * @return Connection连接对象
     * @throws SQLException 抛出异常
     */
    public static Connection getConn() throws SQLException {
        Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
        return conn;
    }

    /**
     * 关闭数据流
     * @param rs 游标对象
     * @param stmt 语句对象
     * @param conn 连接对象
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn){
        try {
            if(rs != null) rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if(stmt != null) stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if(conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

}
